//
//  GYStoreInfoViewController.m
//  GoodLuck
//
//  Created by apple on 2018/3/12.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYStoreInfoViewController.h"
#import "GYNetWorkTools.h"
#import "GYStoreHomeViewController.h"
#import "GYStoreAllViewController.h"
#import "GYStoreNewViewController.h"
#import "GYUser.h"
#import "GYStoreIntroduceViewController.h"
#import "GYStoreSearchMainViewController.h"
#import "GYBasePopoverViewController.h"
#import "GYSearchViewController.h"
#import "PopoverAnimation.h"
#import "GYSystemMessageViewController.h"
#import "GYShopingCartViewController.h"
#import "GYWebViewViewController.h"

@interface GYStoreInfoViewController ()<UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *store_headImageView;
@property (weak, nonatomic) IBOutlet UIImageView *store_ImageView;
@property (weak, nonatomic) IBOutlet UILabel *store_NameLable;
@property (weak, nonatomic) IBOutlet UIButton *store_favoritesBtu;
@property (weak, nonatomic) IBOutlet UIImageView *favoritesImage;


@property (weak, nonatomic) IBOutlet UILabel *store_fansLable;
@property (weak, nonatomic) IBOutlet UILabel *markLable;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *markLableLeftMarginCons;

@property (weak, nonatomic) IBOutlet UIImageView *store_homeImageView;
@property (weak, nonatomic) IBOutlet UIImageView *store_allImageView;
@property (weak, nonatomic) IBOutlet UIImageView *store_newImageView;

@property (weak, nonatomic) IBOutlet UILabel *store_homeLable;
@property (weak, nonatomic) IBOutlet UILabel *store_allLable;
@property (weak, nonatomic) IBOutlet UILabel *store_newLable;


@property (weak, nonatomic) IBOutlet UIButton *store_homeBtu;
@property (weak, nonatomic) IBOutlet UIButton *store_allBtu;
@property (weak, nonatomic) IBOutlet UIButton *store_newBtu;


/** 正在展示的VC */
@property (nonatomic, strong) UIViewController *showingVC;

/** 动画代理 */
@property (nonatomic, strong) PopoverAnimation *animationDelegate;
@end

@implementation GYStoreInfoViewController

- (IBAction)onClick:(UIButton *)sender {
    
    switch (sender.tag) {
        case 1:
        {
            [UIView animateWithDuration:0.5 animations:^{
                self.markLableLeftMarginCons.constant = sender.frame.origin.x;
            }];
            self.store_homeImageView.image = [UIImage imageNamed:@"store_r"];
            self.store_allImageView.image = [UIImage imageNamed:@"全部商品"];
            self.store_newImageView.image = [UIImage imageNamed:@"上新"];
            self.store_homeLable.textColor = RGBColor(0, 158, 214);
            self.store_allLable.textColor = [UIColor blackColor];
            self.store_newLable.textColor = [UIColor blackColor];
        }
            break;
            
        case 2:
        {
            [UIView animateWithDuration:0.5 animations:^{
                self.markLableLeftMarginCons.constant = sender.frame.origin.x;
            }];
            self.store_homeImageView.image = [UIImage imageNamed:@"store_b"];
            self.store_allImageView.image = [UIImage imageNamed:@"goods_r"];
            self.store_newImageView.image = [UIImage imageNamed:@"上新"];
            self.store_homeLable.textColor = [UIColor blackColor];
            self.store_allLable.textColor = RGBColor(0, 158, 214);
            self.store_newLable.textColor = [UIColor blackColor];

        }
            break;
            
        case 3:
        {
            [UIView animateWithDuration:0.5 animations:^{
                self.markLableLeftMarginCons.constant = sender.frame.origin.x;
            }];
            self.store_homeImageView.image = [UIImage imageNamed:@"store_b"];
            self.store_allImageView.image = [UIImage imageNamed:@"全部商品"];
            self.store_newImageView.image = [UIImage imageNamed:@"new_r"];
            self.store_homeLable.textColor = [UIColor blackColor];
            self.store_allLable.textColor = [UIColor blackColor];
            self.store_newLable.textColor = RGBColor(0, 158, 214);

        }
            break;
            
        default:
            break;
    }
    
    //移除其他控制器的View
    [self.showingVC.view removeFromSuperview];
    
    //添加新的控制器的View
    self.showingVC = self.childViewControllers[sender.tag-1];
    self.showingVC.view.frame = CGRectMake(0, 150, ScreenWidth, ScreenHeight-(iPhoneX ? 88 : 64)-150-44);
    //NSLog(@"frame=======%@",NSStringFromCGRect(self.showingVC.view.frame));
    [self.view insertSubview:self.showingVC.view atIndex:0];

}

- (IBAction)storeBtuClick:(UIButton *)sender {
    switch (sender.tag) {
        case 1:
        {
            GYStoreIntroduceViewController *introduceVC  = [[GYStoreIntroduceViewController alloc] initWithNibName:@"GYStoreIntroduceViewController" bundle:nil];
            introduceVC.store_id  = self.store_id;
            __weak typeof(self) weakSelf = self;
            introduceVC.storeFavoritesInofCallBack = ^(BOOL is_favorites, NSInteger num_score) {
                weakSelf.store_favoritesBtu.selected = is_favorites;
                weakSelf.store_fansLable.text = [NSString stringWithFormat:@"%zd",num_score];
                if (is_favorites) {
                    weakSelf.favoritesImage.image = [UIImage imageNamed:@"favorites"];
                }else {
                    weakSelf.favoritesImage.image = [UIImage imageNamed:@"favorite_w"];
                }
            };
            [self.navigationController pushViewController:introduceVC animated:YES];
        }
            break;
            
        case 2:
        {
            NSLog(@"联系客服按钮...");
            GYWebViewViewController *WebCustomerServiceVC = [[GYWebViewViewController alloc] initWithNibName:@"GYWebViewViewController" bundle:nil];
            WebCustomerServiceVC.url = [NSURL URLWithString:@"https://youwei.yunhongkg.com/appapi/index.php?act=message&op=Customer"];
            [self.navigationController pushViewController:WebCustomerServiceVC animated:YES];
        }
            break;
            
        default:
            break;
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化
    [self setupUI];
    
    //初始化子控制器
    [self addChildViewControllers];
}
- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    //Xib创建的视图 在ViewDidLoad方法中还没有布局  所以设置frame会不准却
    self.showingVC.view.frame = CGRectMake(0, 150, ScreenWidth, ScreenHeight-(iPhoneX ? 88 : 64)-150-44);
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    NSLog(@"showingVC.view.frame===========%@",NSStringFromCGRect(self.showingVC.view.frame));
    NSLog(@"storeInfo.View.frame===========%@",NSStringFromCGRect(self.view.frame));
}

- (void)setupUI {
    //1.设置搜索框
    UISearchBar *searchBar = [[UISearchBar alloc] init];
    searchBar.delegate = self;
    searchBar.frame = CGRectMake(0, 0, 130, 40);
    searchBar.backgroundImage = [[UIImage alloc] init];
    UITextField *searchField = [searchBar valueForKey:@"searchField"];
    searchField.layer.borderColor = RGBColor(152, 152, 152).CGColor;
    searchBar.placeholder = @"请输入关键词";
    self.navigationItem.titleView = searchBar;
    
    //2.设置导航栏
    UIBarButtonItem *moreItem = [UIBarButtonItem itemWithImage:@"more_b" tagret:self action:@selector(moreBtuClick) button:nil];
    self.navigationItem.rightBarButtonItems = @[moreItem];
    
}

- (void)addChildViewControllers {
    GYStoreHomeViewController *homeVC = [[GYStoreHomeViewController alloc] init];
    homeVC.store_id = self.store_id;
    __weak typeof(self) weakSelf = self;
    homeVC.storeInfoCallback = ^(NSDictionary *store_info) {
        //NSLog(@"storeinf=======%@",store_info);
        [weakSelf.store_headImageView sd_setImageWithURL:[NSURL URLWithString:[store_info objectForKey:@"mb_title_img"]] placeholderImage:[UIImage imageNamed:@""]];
        [weakSelf.store_ImageView sd_setImageWithURL:[NSURL URLWithString:[store_info objectForKey:@"store_avatar"]] placeholderImage:[UIImage imageNamed:@"4"]];
        weakSelf.store_NameLable.text = [store_info objectForKey:@"store_name"];
        BOOL is_favorate = [[store_info objectForKey:@"is_favorate"] boolValue];
        weakSelf.store_favoritesBtu.selected = is_favorate;
        if (is_favorate) {
            weakSelf.favoritesImage.image = [UIImage imageNamed:@"favorites"];
        }else {
            weakSelf.favoritesImage.image = [UIImage imageNamed:@"favorite_w"];
        }
        weakSelf.store_fansLable.text = [NSString stringWithFormat:@"%d",[[store_info objectForKey:@"store_collect"] intValue]];
    };
    
    GYStoreAllViewController *allVC = [[GYStoreAllViewController alloc] init];
    allVC.store_id = self.store_id;
    
    GYStoreNewViewController *newVC = [[GYStoreNewViewController alloc] init];
    newVC.store_id = self.store_id;
    
    [self addChildViewController:homeVC];
    [self addChildViewController:allVC];
    [self addChildViewController:newVC];
    
    self.showingVC = homeVC;
    [self.view addSubview:self.showingVC.view];
}

#pragma mark -- 按钮点击事件

- (void)moreBtuClick {
    GYBasePopoverViewController *basePopoverVC = [[GYBasePopoverViewController alloc] initWithNibName:@"GYBasePopoverViewController" bundle:nil];
    __weak typeof(self) weakSelf = self;
    basePopoverVC.popoverViewSelectIndexCallBack = ^(NSInteger index) {
        switch (index) {
            case 0:
            {
                weakSelf.tabBarController.selectedIndex = 0;
                [weakSelf.tabBarController.tabBar setHidden:NO];
            }
                break;
                
            case 1:
            {
                GYSearchViewController *searchVC = [[GYSearchViewController alloc] init];
                [self.navigationController pushViewController:searchVC animated:YES];
            }
                break;
                
            case 2:
            {
                if ([GYUser loadUserInfo] != nil) {
                    GYShopingCartViewController *shopingCartVC = [[GYShopingCartViewController alloc] initWithNibName:@"GYShopingCartViewController" bundle:nil];
                    [weakSelf.navigationController pushViewController:shopingCartVC animated:YES];
                }else {
                    weakSelf.tabBarController.selectedIndex = 2;
                    [weakSelf.tabBarController.tabBar setHidden:YES];
                }
            }
                break;
                
            case 3:
            {
                if ([GYUser loadUserInfo] != nil) {
                    GYSystemMessageViewController *systemMessageVC = [[GYSystemMessageViewController alloc] initWithNibName:@"GYSystemMessageViewController" bundle:nil];
                    [weakSelf.navigationController pushViewController:systemMessageVC animated:YES];
                }else {
                    weakSelf.tabBarController.selectedIndex = 2;
                    [weakSelf.tabBarController.tabBar setHidden:YES];
                }
            }
                break;
                
            default:
                break;
        }
    };
    basePopoverVC.titles = @[@"首页",@"搜索",@"购物车",@"消息"];
    basePopoverVC.images = @[@"home_w",@"search_w",@"ff_icon3",@"message_w"];
    
    self.animationDelegate = [[PopoverAnimation alloc] init];
    self.animationDelegate.popoverFrame = CGRectMake(ScreenWidth-widthScale(130, iphone6_W)-5, 50, widthScale(130, iphone6_W), basePopoverVC.titles.count*50);
    basePopoverVC.transitioningDelegate = (id)weakSelf.animationDelegate;
    
    basePopoverVC.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:basePopoverVC animated:YES completion:nil];

}


/**
 店铺收藏按钮点击事件

 @param sender <#sender description#>
 */
- (IBAction)storeFavoritesBtuClick:(UIButton *)sender {
    if ([GYUser loadUserInfo] == nil) {
        //跳转到登陆界面
        self.tabBarController.selectedIndex = 2;
        [self.tabBarController.tabBar setHidden:YES];
    }else {
        int num = [self.store_fansLable.text intValue];
        if (sender.selected) {
            //代表已经收藏    点击就是取消收藏的请求
            [GYNetWorkTools requestCancelStoreFavorites:self.store_id success:^(NSDictionary *obj) {
                //NSLog(@"cacanel=====%@",obj);
                int code = [[obj objectForKey:@"code"] intValue];
                if (code == 200) {
                    [EasyTextView showSuccessText:@"取消收藏成功!"];
                    sender.selected = !sender.selected;
                    self.favoritesImage.image = [UIImage imageNamed:@"favorite_w"];
                    self.store_fansLable.text = [NSString stringWithFormat:@"%d",num-1];
                    
                }else if (code == 400) {
                    [EasyTextView showText:@"取消收藏失败!"];
                    NSLog(@"取消收藏失败...");
                }
            } error:^(NSError *error) {
                [EasyTextView showText:@"取消收藏失败!"];
                NSLog(@"loadCancelStoreFavoritesFailed====%@",error);
            }];
            
        }else {
            [GYNetWorkTools requestStoreFavorites:self.store_id success:^(NSDictionary *obj) {
                //NSLog(@"add=====%@",obj);
                int code = [[obj objectForKey:@"code"] intValue];
                if (code == 200) {
                    [EasyTextView showSuccessText:@"收藏成功!"];
                    self.favoritesImage.image = [UIImage imageNamed:@"favorites"];
                    
                    sender.selected = !sender.selected;
                    self.store_fansLable.text = [NSString stringWithFormat:@"%d",num+1];
                }else if (code == 400) {
                    [EasyTextView showText:@"收藏失败!"];
                    NSLog(@"收藏失败...");
                }
            } error:^(NSError *error) {
                [EasyTextView showText:@"收藏失败!"];
                NSLog(@"loadStoreFavoritesFailed====%@",error);
            }];
        }
    }
}


#pragma mark -- UISearchBarDelegate

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
    GYStoreSearchMainViewController *searchMainVC = [[GYStoreSearchMainViewController alloc] initWithNibName:@"GYStoreSearchMainViewController" bundle:nil];
    searchMainVC.store_id = self.store_id;
    [self.navigationController pushViewController:searchMainVC animated:YES];

}

- (void)dealloc {
    NSLog(@"GYStoreInfoViewController=====dealloc");
}


@end
